<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

	<script type="text/javascript" src="${pageContext.request.contextPath}/Js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/Js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/Js/easyui/locale/easyui-lang-zh_CN.js"></script>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/Js/easyui/themes/icon.css">
	<link id="themeLink" rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/Js/easyui/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/Styles/classify.css">
	<link href="${pageContext.request.contextPath}/Styles/main.css" rel="stylesheet" type="text/css" />
	
	<style type="text/css">
		.cate{
			margin-top:30px;
			width: 300px;
			height:300px;
			border:1px solid red;
		}
		
		.cate ul li{
			
			margin-top:5px;
		
		}
	</style>
</head>
<body>
	
	<h1>
	    <span class="action-span1"><a href="__GROUP__">博客管理中心</a></span>
	    <span id="search_id" class="action-span1"> - 博客分类管理</span>
	    <div style="clear:both"></div>
	</h1>
	
	<div class="cate">
	 	<ul id="contentCategory" class="easyui-tree">
    	</ul>
	</div>
	
	<div id="contentCategoryMenu" class="easyui-menu" style="width:120px;" data-options="onClick:menuHandler">
	    <div data-options="iconCls:'icon-add',name:'add'">添加</div>
	    <div data-options="iconCls:'icon-remove',name:'rename'">重命名</div>
	    <div class="menu-sep"></div>
	    <div data-options="iconCls:'icon-remove',name:'delete'">删除</div>
	</div>
	
</body>

<script type="text/javascript">


 $(function(){
	$("#contentCategory").tree({
		url : '/manage/category/list',
		animate: true,
		method : "GET",
		onContextMenu: function(e,node){
            e.preventDefault();
            $(this).tree('select',node.target);
            $('#contentCategoryMenu').menu('show',{
                left: e.pageX,
                top: e.pageY
            });
        },
        onAfterEdit : function(node){
        	var _tree = $(this);
        	if(node.id == 0){
        		// 新增节点 
        		$.post("/manage/category/add_category",{parentId:node.parentId,name:node.text},function(data){
        			if(data.status == 0){
        				$.messager.alert('提示','添加分类成功!');
        				_tree.tree("update",{
            				target : node.target,
            				id : data.data
            			});
        			}else{
        				$.messager.alert('提示','创建'+node.text+' 分类失败!');
        			}
        		});
        	}else{
        		$.post("/manage/category/update_category_name",{id:node.id,name:node.text},function(data){
        			if(data.status == 0){
        				$.messager.alert('提示','更新品类成功!');
        			}
        		});
        		
        	}
        }
	});
});
 
function menuHandler(item){
	var tree = $("#contentCategory");
	var node = tree.tree("getSelected");
	if(item.name === "add"){
		tree.tree('append', {
            parent: (node?node.target:null),
            data: [{
                text: '新建分类',
                id : 0,
                parentId : node.id
            }]
        }); 
		var _node = tree.tree('find',0);
		tree.tree("select",_node.target).tree('beginEdit',_node.target);
	}else if(item.name === "rename"){
		tree.tree('beginEdit',node.target);
	}else if(item.name === "delete"){
		$.messager.confirm('确认','确定删除名为 '+node.text+' 的分类吗？',function(r){
			if(r){
				$.post("/manage/category/delete",{parentId:node.parentId,id:node.id},function(data){
					if(data.status == 0 ){
						$.messager.alert('提示','删除品类成功!');
					}
					tree.tree("remove",node.target);
				});	
			}
		});
	}
}
</script>
</html>